<template>
  <div>
    <div style="display: flex; justify-content: center; margin-top: 15px">
      <el-input v-model="keyWords" style="width: 40%; margin-right: 10px;"
                placeholder="请输入用户名搜索用户..."
                prefix-icon="el-icon-serarch"></el-input>
      <el-button type="danger" icon="el-icon-search" @click="doSearch">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="(admin, index) in admins" :key="index">
        <template #header>
          <div class="card-header">
            <span>{{ admin.name }}</span>
            <el-button type="text" icon="el-icon-delete" style="color: #dc191d" @click="deleteAdmin(admin)"></el-button>
          </div>
        </template>
        <div>
          <div class="img-container">
            <img class="img-userFace" :src="admin.userFace" :title="admin.name" :alt="admin.name"/>
          </div>
          <div class="userinfo-container">
            <table >
              <tr>
                <td>用户名：</td>
                <td>{{ admin.name }}</td>
              </tr>
              <tr>
                <td>手机号：</td>
                <td>{{ admin.phone }}</td>
              </tr>
              <tr>
                <td>电话号码：</td>
                <td>{{ admin.telephone }}</td>
              </tr>
              <tr>
                <td>地址：</td>
                <td>{{ admin.address }}</td>
              </tr>
              <tr>
                <td>用户状态：</td>
                <td><el-switch
                    @change="enabledChange(admin)"
                    v-model="admin.enabled"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-text="启用"
                    inactive-text="禁用">
                </el-switch></td>
              </tr>
            </table>
            <div>角色:
              <el-tag type="warning"
                      style="margin: 2px"
                      :key="idx"
                      v-for="(role, idx) in admin.roles">{{ role.nameZh }}
              </el-tag>
              <el-popover
                  placement="right"
                  title="角色列表"
                  :width="200"
                  @show="showPop(admin)"
                  @hide="hidePop(admin)"
                  trigger="click">
                <el-select v-model="selectedRoles" multiple placeholder="请选择">
                  <el-option
                      v-for="(r, index) in allRoles"
                      :key="index"
                      :label="r.nameZh"
                      :value="r.id">
                  </el-option>
                </el-select>
                <template #reference>
                  <el-button style="margin: 2px" icon="el-icon-more" type="text"></el-button>
                </template>
              </el-popover>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "SysAdmin",
  data() {
    return {
      admins: [],
      keyWords: '',
      selectedRoles: [],
      allRoles: []
    }
  },
  mounted() {
    this.initAdmin();
  },

  methods: {
    hidePop(admin) {
      let flag = false;
      let roles = [];
      Object.assign(roles, admin.roles);
      if (roles.length != this.selectedRoles.length) {
        flag = true;
      } else {
        for (let i = 0; i < roles.length; i++) {
          let r = roles[i];
          for (let j = 0; j < this.selectedRoles.length; j++) {
            if (r.id == this.selectedRoles[j]) {
              roles.splice(i, 1);
              i--;
              break;
            }
          }
        }
        if (roles.length != 0) {
          flag = true;
        }
      }

      if (flag) {
        let url = '/system/admin/role?adminId=' + admin.id;
        this.selectedRoles.forEach(rid => {
          url += '&rIds=' + rid
        });
        this.putRequest(url).then(resp => {
          if (resp) {
            this.initAdmin();
          }
        });
      }
    },
    showPop(admin) {
      this.selectedRoles = [];
      let roles = admin.roles;
      roles.forEach(role => {
        this.selectedRoles.push(role.id);
      });
      this.initAllRoles();
    },
    initAllRoles() {
      this.getRequest('/system/admin/roles').then(resp => {
        if (resp) {
          this.allRoles = resp.obj;
        }
      });
    },
    enabledChange(admin) {
      console.log(admin.enabled)
      this.putRequest('/system/admin/', admin).then(resp => {
        if (resp) {
          console.log(resp)
          this.initAdmin();
        }
      });
    },
    deleteAdmin(admin) {
      this.$confirm('此操作将永久删除 ['+admin.name+'] 操作员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/admin/'+admin.id).then(resp => {
          this.initAdmin();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    doSearch() {
      this.initAdmin()
    },
    initAdmin() {
      this.getRequest('/system/admin/?keyWords=' + this.keyWords).then(resp => {
        if (resp) {
          this.admins = resp.obj;
        }
      });
    },
  }
}
</script>

<style>
.admin-container {
  display: flex;
  margin: 15px;
  justify-content: space-around;
  flex-wrap: wrap;
}

.admin-card {
  width: 30%;
  margin-bottom: 10px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.img-container {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.img-userFace {
  width: 72px;
  height: 72px;
  border-radius: 72px;
}

.userinfo-container {
  font-size: 15px;
  font-family: 宋体;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}
</style>